﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DesignPatternDemoCApp.AlgorithmicDemo
{
    /// <summary>
    /// 过河的用户信息
    /// </summary>
    public class User
    {
        /// <summary>
        /// 名字
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 单独过河时间x分钟
        /// </summary>
        public int Time { get; set; }
    }

    public class CrossRiverTest
    {
        static User[] pu = null;
        static int totalTime = 0;

        //public static void Main(string[] args)
        //{
        //    pu = new User[6];
        //    pu[0] = new User { Name = "T1", Time = 1 };
        //    pu[1] = new User { Name = "T2", Time = 5 };
        //    pu[2] = new User { Name = "T3", Time = 6 };
        //    pu[3] = new User { Name = "T4", Time = 7 };
        //    pu[4] = new User { Name = "T5", Time = 8 };
        //    pu[5] = new User { Name = "T6", Time = 9 };
        //    Time(pu.Length);
        //    Console.WriteLine($"总共耗时：{totalTime}");
        //    Console.Read();
        //}


        public static void Time(int n)
        {
            if (n == 1)
            {
                Console.WriteLine($"[{pu[0].Name}]:{pu[0].Time}->");
                totalTime += pu[0].Time;
                return;
            }
            if (n == 2)
            {
                Console.WriteLine($"[{pu[0].Name},{pu[1].Name}]:{pu[1].Time}->");
                totalTime += pu[1].Time;
                return;
            }
            if (n == 3)
            {
                Console.WriteLine($"[{pu[0].Name},{pu[1].Name}]:{pu[1].Time}->");
                Console.WriteLine($"[{pu[1].Name}]:{pu[1].Time}<-");
                Console.WriteLine($"[{pu[1].Name},{pu[2].Name}]:{pu[2].Time}->");
                totalTime += pu[0].Time + pu[1].Time + pu[2].Time;
                return;
            }
            //2倍次快>最快+次慢
            //这两种方案的末状态一定是：最大的两个已经过河，最小的两个留在河这面
            //剩下的又回到了n-2个人的初始状态
            if (pu[1].Time * 2 >= pu[0].Time + pu[n - 2].Time)
            {

                //最快和次慢过去
                Console.WriteLine($"[{pu[0].Name},{pu[n - 2].Name}]:{pu[n - 2].Time}->");
                //最快回来
                Console.WriteLine($"[{pu[0].Name}]:{pu[0].Time}<-");
                //最快和最慢过去
                Console.WriteLine($"[{pu[0].Name},{pu[n - 1].Name}]:{pu[n - 1].Time}->");
                //最快回来
                Console.WriteLine($"[{pu[0].Name}]:{pu[0].Time}<-");
                totalTime += 2 * pu[0].Time + pu[n - 1].Time + pu[n - 2].Time;
                //回到n-2个人的原始状态
                Time(n - 2);
                return;
            }
            else
            {
                //最快和次快过去
                Console.WriteLine($"[{pu[0].Name},{pu[1].Name}]:{pu[1].Time}->");
                //次快回来
                Console.WriteLine($"[{pu[1].Name}]:{pu[1].Time}<-");
                //次慢和最慢过去
                Console.WriteLine($"[{pu[n - 2].Name},{pu[n - 1].Name}]:{pu[n - 1].Time}->");
                //最快回来
                Console.WriteLine($"[{pu[0].Name}]:{pu[0].Time}<-");
                //回到n-2个人的原始状态
                totalTime += 2 * pu[1].Time + pu[0].Time + pu[n - 1].Time;
                Time(n - 2);
                return;
            }
        }
    }
}
